package com.dhj.streamTest2;

import com.dhj.streamTest.entity.Author;

import java.util.List;
import java.util.Optional;
import java.util.function.BinaryOperator;

import static com.dhj.streamTest.entity.AuthorList.getAuthors;

public class Demo8 {

    public static void main(String[] args) {
        List<Author> authors = getAuthors();

        Integer reduce = authors.stream().distinct()
                .map(author -> author.getAge())
                .reduce(0, new BinaryOperator<Integer>() {
                    @Override
                    public Integer apply(Integer integer, Integer integer2) {
                        return integer + integer2;
                    }
                });

        System.out.println("所有作者的年龄和：" + reduce);

        System.out.println("-------------------------------");

        Optional<Integer> reduce1 = authors.stream().distinct()
                .map(author -> author.getAge())
                .reduce((age1, age2) -> age1 + age2);

        System.out.println("所有作者的年龄和：" + reduce1.get());


        System.out.println("-------------------------------");


        Integer reduce2 = authors.stream().distinct()
                .map(author -> author.getAge())
                .reduce(0, (age1, age2) -> age1 + age2);

        System.out.println("所有作者的年龄和：" + reduce2);

    }
}
